package jdbc;

import java.sql.*;
import java.util.Date;

/**
 * 测试时间处理(java.sql.Date,Time,Timestamp)
 */
public class Demo07 {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            //加载驱动类
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?serverTimezone=Asia/Shanghai","root","123456");

            String sql="insert into t_user (username,pwd,regTime) values (?,?,?)";//?是占位符
            ps=conn.prepareStatement(sql);
            ps.setString(1,"lqk");
            ps.setString(2,"123456");
            ps.setTimestamp(3,new Timestamp(System.currentTimeMillis()));
            ps.executeUpdate();


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
